package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesSaleExpressDetailRpcService;
import com.hunttown.mes.rpc.domain.MesSaleExpressDetailDTO;
import com.hunttown.mes.rpc.domain.query.MesSaleExpressDetailDTOQuery;
import com.hunttown.mes.service.MesSaleExpressDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesSaleExpressDetailConverts.convert;

/**
 * created by wangjunfu on 2022-07-25 14:54:21
 */
@Service
public class MesSaleExpressDetailRpcServiceImpl implements MesSaleExpressDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesSaleExpressDetailRpcServiceImpl.class);

    private final MesSaleExpressDetailService mesSaleExpressDetailService;

    @Autowired
    public MesSaleExpressDetailRpcServiceImpl(MesSaleExpressDetailService mesSaleExpressDetailService) {
        this.mesSaleExpressDetailService = mesSaleExpressDetailService;
    }

    @Override
    public MesSaleExpressDetailDTO insert(MesSaleExpressDetailDTO mesSaleExpressDetailDTO) {
        try {
            return convert(mesSaleExpressDetailService.insert(convert(mesSaleExpressDetailDTO)));
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail create failed.", e);
            throw new RuntimeException("MesSaleExpressDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesSaleExpressDetailDTO mesSaleExpressDetailDTO) {
        try {
            return mesSaleExpressDetailService.updateInfoById(convert(mesSaleExpressDetailDTO));
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail update by id failed.", e);
            throw new RuntimeException("MesSaleExpressDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesSaleExpressDetailDTOQuery mesSaleExpressDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleExpressDetailDTOQuery.getW_id() != null && mesSaleExpressDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", mesSaleExpressDetailDTOQuery.getW_id());
            }

            if (mesSaleExpressDetailDTOQuery.getIds() != null && !mesSaleExpressDetailDTOQuery.getIds().equals("")) {
                query.put("ids", mesSaleExpressDetailDTOQuery.getIds());
            }

            if (mesSaleExpressDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleExpressDetailDTOQuery.getStopFlag());
            }

            if (mesSaleExpressDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleExpressDetailDTOQuery.getDeleteFlag());
            }

            return mesSaleExpressDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail update by query failed.", e);
            throw new RuntimeException("MesSaleExpressDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleExpressDetailDTO selectById(Integer id) {
        try {
            return convert(mesSaleExpressDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail select by id failed.", e);
            throw new RuntimeException("MesSaleExpressDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleExpressDetailDTO selectObjByQuery(MesSaleExpressDetailDTOQuery mesSaleExpressDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleExpressDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleExpressDetailDTOQuery.getStopFlag());
            }

            if (mesSaleExpressDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleExpressDetailDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getOrderBy())) {
                query.put("orderBy", mesSaleExpressDetailDTOQuery.getOrderBy());
            }

            return convert(mesSaleExpressDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail select obj by query failed.", e);
            throw new RuntimeException("MesSaleExpressDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesSaleExpressDetailDTO> getListForPage(MesSaleExpressDetailDTOQuery mesSaleExpressDetailDTOQuery) {
        Query query = Query.page(mesSaleExpressDetailDTOQuery.getPn(), mesSaleExpressDetailDTOQuery.getPageSize());

        if (mesSaleExpressDetailDTOQuery.getId() != null && mesSaleExpressDetailDTOQuery.getId() > 0) {
            query.put("id", mesSaleExpressDetailDTOQuery.getId());
        }

        if (mesSaleExpressDetailDTOQuery.getIds() != null && !mesSaleExpressDetailDTOQuery.getIds().equals("")) {
            query.put("ids", mesSaleExpressDetailDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getLikeName())) {
            query.put("likeName", mesSaleExpressDetailDTOQuery.getLikeName());
        }

        if (mesSaleExpressDetailDTOQuery.getExpressId() != null) {
            query.put("expressId", mesSaleExpressDetailDTOQuery.getExpressId());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getShowDate())) {
            query.put("showDate", mesSaleExpressDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getStartDate())) {
            query.put("startDate", mesSaleExpressDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getEndDate())) {
            query.put("endDate", mesSaleExpressDetailDTOQuery.getEndDate());
        }

        if (mesSaleExpressDetailDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesSaleExpressDetailDTOQuery.getStopFlag());
        }

        if (mesSaleExpressDetailDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesSaleExpressDetailDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", mesSaleExpressDetailDTOQuery.getOrderBy());
        }

        List<MesSaleExpressDetailDTO> list = convert(mesSaleExpressDetailService.selectByQuery(query));
        Page<MesSaleExpressDetailDTO> page = Page.from(mesSaleExpressDetailDTOQuery.getPn(), mesSaleExpressDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(mesSaleExpressDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesSaleExpressDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail delete failed.", e);
            throw new RuntimeException("MesSaleExpressDetail delete failed, Please order your code.", e);
        }
    }

    @Override
    public Long getProductSendCount(Integer productId) {
        try {
            return mesSaleExpressDetailService.getProductSendCount(productId);
        } catch (Exception e) {
            logger.error("MesSaleExpressDetail get product send count failed.", e);
            throw new RuntimeException("MesSaleExpressDetail get product send count failed, Please order your code.", e);
        }
    }
}